package com.spring.test.factory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 静态工厂方法:直接调用某一个类的静态方法就可以返还Bean的实例
 */
public class StaticCarFactory {
	private static Map<String,Car> cars=new HashMap<String,Car>();
	private static Map<String,ElectricCar> electricCars=new HashMap<>();
	static {
		cars.put("audi",new Car("audi",300000));
		cars.put("ford",new Car("ford",400000));
		electricCars.put("bmw",new ElectricCar("bmw",500000));
	}

	public static Car getCar(String name) {
		return cars.get(name);
	}

/*
	public static Car getCar(String name,Double price) {
		Optional<Car> optional = cars.values().stream().filter(car -> car.equals(new Car(name, price))).findFirst();
		if(optional.isPresent()){
			return optional.get();
		}
		return null;
	}
*/


	public static <C extends ElectricCar> C getElectricCar(String name){
		return (C)electricCars.get(name);
	}


	/*
		public static <C extends ElectricCar> C getElectricCar(Car target){
			Optional<ElectricCar> optional = electricCars.values().stream().filter(electricCar ->
			{
				return electricCar.equals(target);
			}).findFirst();
			if(optional.isPresent()){
				return (C)optional.get();
			}
			return null;
		}
	*/
}
